{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T14:48:20.777281Z",
     "start_time": "2018-02-02T14:48:20.774099Z"
    }
   },
   "source": [
    "## 问题： 求y = w^2 - 10*w +25的最小值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T15:32:46.734043Z",
     "start_time": "2018-02-02T15:32:46.728847Z"
    }
   },
   "source": [
    "## tensorflow求导的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-04T01:03:25.560509Z",
     "start_time": "2018-02-04T01:03:25.555532Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-04T01:03:25.873147Z",
     "start_time": "2018-02-04T01:03:25.853325Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "w = tf.Variable(0, dtype=tf.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-04T01:03:26.814858Z",
     "start_time": "2018-02-04T01:03:26.223400Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n",
      "100.0\n"
     ]
    }
   ],
   "source": [
    "cost = tf.add(tf.add(w**2, tf.multiply(-10., w)), 100.0)\n",
    "train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "session = tf.Session()\n",
    "session.run(init)\n",
    "print(session.run(w))\n",
    "print(session.run(cost))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-04T01:03:27.263777Z",
     "start_time": "2018-02-04T01:03:26.817140Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100.0\n",
      "99.01\n",
      "98.0592\n",
      "97.1461\n",
      "96.2691\n",
      "95.4268\n",
      "94.6179\n",
      "93.841\n",
      "93.0949\n",
      "92.3784\n",
      "91.6902\n",
      "91.0293\n",
      "90.3945\n",
      "89.7849\n",
      "89.1994\n",
      "88.6371\n",
      "88.0971\n",
      "87.5784\n",
      "87.0803\n",
      "86.6019\n",
      "86.1425\n",
      "85.7013\n",
      "85.2775\n",
      "84.8705\n",
      "84.4796\n",
      "84.1042\n",
      "83.7437\n",
      "83.3975\n",
      "83.0649\n",
      "82.7456\n",
      "82.4388\n",
      "82.1443\n",
      "81.8613\n",
      "81.5896\n",
      "81.3287\n",
      "81.0781\n",
      "80.8374\n",
      "80.6062\n",
      "80.3842\n",
      "80.171\n",
      "79.9662\n",
      "79.7696\n",
      "79.5807\n",
      "79.3993\n",
      "79.2251\n",
      "79.0578\n",
      "78.8971\n",
      "78.7428\n",
      "78.5945\n",
      "78.4522\n",
      "78.3155\n",
      "78.1842\n",
      "78.0581\n",
      "77.937\n",
      "77.8207\n",
      "77.709\n",
      "77.6017\n",
      "77.4987\n",
      "77.3997\n",
      "77.3047\n",
      "77.2134\n",
      "77.1258\n",
      "77.0416\n",
      "76.9608\n",
      "76.8831\n",
      "76.8085\n",
      "76.7369\n",
      "76.6681\n",
      "76.6021\n",
      "76.5386\n",
      "76.4777\n",
      "76.4192\n",
      "76.363\n",
      "76.309\n",
      "76.2572\n",
      "76.2074\n",
      "76.1596\n",
      "76.1137\n",
      "76.0696\n",
      "76.0272\n",
      "75.9865\n",
      "75.9475\n",
      "75.9099\n",
      "75.8739\n",
      "75.8393\n",
      "75.8061\n",
      "75.7742\n",
      "75.7435\n",
      "75.7141\n",
      "75.6858\n",
      "75.6586\n",
      "75.6325\n",
      "75.6075\n",
      "75.5834\n",
      "75.5603\n",
      "75.5381\n",
      "75.5168\n",
      "75.4964\n",
      "75.4767\n",
      "75.4578\n",
      "75.4397\n",
      "75.4223\n",
      "75.4056\n",
      "75.3895\n",
      "75.3741\n",
      "75.3593\n",
      "75.345\n",
      "75.3314\n",
      "75.3183\n",
      "75.3056\n",
      "75.2935\n",
      "75.2819\n",
      "75.2708\n",
      "75.26\n",
      "75.2497\n",
      "75.2398\n",
      "75.2303\n",
      "75.2212\n",
      "75.2125\n",
      "75.2041\n",
      "75.196\n",
      "75.1882\n",
      "75.1808\n",
      "75.1736\n",
      "75.1667\n",
      "75.1601\n",
      "75.1538\n",
      "75.1477\n",
      "75.1418\n",
      "75.1362\n",
      "75.1308\n",
      "75.1257\n",
      "75.1207\n",
      "75.1159\n",
      "75.1113\n",
      "75.1069\n",
      "75.1027\n",
      "75.0986\n",
      "75.0947\n",
      "75.091\n",
      "75.0873\n",
      "75.0839\n",
      "75.0806\n",
      "75.0774\n",
      "75.0743\n",
      "75.0714\n",
      "75.0685\n",
      "75.0658\n",
      "75.0632\n",
      "75.0607\n",
      "75.0583\n",
      "75.056\n",
      "75.0538\n",
      "75.0517\n",
      "75.0496\n",
      "75.0476\n",
      "75.0458\n",
      "75.0439\n",
      "75.0422\n",
      "75.0405\n",
      "75.0389\n",
      "75.0374\n",
      "75.0359\n",
      "75.0345\n",
      "75.0331\n",
      "75.0318\n",
      "75.0305\n",
      "75.0293\n",
      "75.0282\n",
      "75.0271\n",
      "75.026\n",
      "75.025\n",
      "75.024\n",
      "75.023\n",
      "75.0221\n",
      "75.0212\n",
      "75.0204\n",
      "75.0196\n",
      "75.0188\n",
      "75.0181\n",
      "75.0173\n",
      "75.0167\n",
      "75.016\n",
      "75.0154\n",
      "75.0148\n",
      "75.0142\n",
      "75.0136\n",
      "75.0131\n",
      "75.0126\n",
      "75.0121\n",
      "75.0116\n",
      "75.0111\n",
      "75.0107\n",
      "75.0103\n",
      "75.0099\n",
      "75.0095\n",
      "75.0091\n",
      "75.0087\n",
      "75.0084\n",
      "75.008\n",
      "75.0077\n",
      "75.0074\n",
      "75.0071\n",
      "75.0069\n",
      "75.0066\n",
      "75.0063\n",
      "75.0061\n",
      "75.0058\n",
      "75.0056\n",
      "75.0054\n",
      "75.0052\n",
      "75.005\n",
      "75.0048\n",
      "75.0046\n",
      "75.0044\n",
      "75.0042\n",
      "75.0041\n",
      "75.0039\n",
      "75.0037\n",
      "75.0036\n",
      "75.0034\n",
      "75.0033\n",
      "75.0032\n",
      "75.0031\n",
      "75.0029\n",
      "75.0028\n",
      "75.0027\n",
      "75.0026\n",
      "75.0025\n",
      "75.0024\n",
      "75.0023\n",
      "75.0022\n",
      "75.0021\n",
      "75.002\n",
      "75.002\n",
      "75.0019\n",
      "75.0018\n",
      "75.0017\n",
      "75.0017\n",
      "75.0016\n",
      "75.0015\n",
      "75.0015\n",
      "75.0014\n",
      "75.0014\n",
      "75.0013\n",
      "75.0013\n",
      "75.0012\n",
      "75.0012\n",
      "75.0011\n",
      "75.0011\n",
      "75.001\n",
      "75.001\n",
      "75.0009\n",
      "75.0009\n",
      "75.0009\n",
      "75.0008\n",
      "75.0008\n",
      "75.0008\n",
      "75.0007\n",
      "75.0007\n",
      "75.0007\n",
      "75.0007\n",
      "75.0006\n",
      "75.0006\n",
      "75.0006\n",
      "75.0006\n",
      "75.0005\n",
      "75.0005\n",
      "75.0005\n",
      "75.0005\n",
      "75.0005\n",
      "75.0004\n",
      "75.0004\n",
      "75.0004\n",
      "75.0004\n",
      "75.0004\n",
      "75.0004\n",
      "75.0003\n",
      "75.0003\n",
      "75.0003\n",
      "75.0003\n",
      "75.0003\n",
      "75.0003\n",
      "75.0003\n",
      "75.0003\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0002\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0001\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "75.0\n",
      "4.99999\n",
      "75.0\n"
     ]
    }
   ],
   "source": [
    "for i in range(1000):\n",
    "    _, c = session.run([train, cost])\n",
    "#     print(_)\n",
    "    print(c)\n",
    "print(session.run(w))  # 1000次迭代后w的取值\n",
    "print(session.run(cost))  # 算出此时cost的值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:03:07.337489Z",
     "start_time": "2018-02-02T23:03:07.331152Z"
    }
   },
   "source": [
    "## 注入data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:15:16.381152Z",
     "start_time": "2018-02-03T00:15:16.367101Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "w = tf.Variable(0, dtype=tf.float32)\n",
    "coeficients = np.array([[1.], [-10.], [26.]])\n",
    "x = tf.placeholder(tf.float32, [3, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:15:17.202205Z",
     "start_time": "2018-02-03T00:15:16.735151Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n",
      "0.1\n"
     ]
    }
   ],
   "source": [
    "cost = x[0][0] * w **2 + x[1][0] * w + x[2][0]\n",
    "train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)\n",
    "init = tf.global_variables_initializer()\n",
    "session = tf.Session()\n",
    "session.run(init)\n",
    "print(session.run(w))\n",
    "\n",
    "session.run(train, feed_dict={x: coeficients})\n",
    "print(session.run(w))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:15:39.936003Z",
     "start_time": "2018-02-03T00:15:39.509393Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.99999\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "for i in range(1000):\n",
    "    session.run(train, feed_dict={x: coeficients})\n",
    "print(session.run(w))\n",
    "print(session.run(cost, feed_dict={x:coeficients}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## mxnet求导的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:15:58.619042Z",
     "start_time": "2018-02-02T23:15:56.875236Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import mxnet.ndarray as nd\n",
    "import mxnet.autograd as ag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:15:58.628791Z",
     "start_time": "2018-02-02T23:15:58.620809Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = nd.array([0])\n",
    "x.attach_grad()  # 当进行求导的时候，我们需要一个地方来存x的导数，这个可以通过NDArray的方法attach_grad()来要求系统申请对应的空间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:15:58.635639Z",
     "start_time": "2018-02-02T23:15:58.630816Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def SGD(x, lr):\n",
    "    x[:] = x - lr * x.grad\n",
    "    \n",
    "def f(x):\n",
    "    return x ** 2 - 10 * x + 25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:16:00.857445Z",
     "start_time": "2018-02-02T23:16:00.327727Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(1000):\n",
    "    with ag.record(): # 默认情况下, MXNet不会自动记录和构建用于求导的计算图，我们需要使用autograd里的record()函数来显式的要求MXNet记录我们需要求导的程序。\n",
    "        y = f(x)\n",
    "    y.backward()\n",
    "    SGD(x, 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:18:13.066475Z",
     "start_time": "2018-02-02T23:18:13.057235Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "[ 4.99998856]\n",
      "<NDArray 1 @cpu(0)>\n",
      "\n",
      "[ -2.28881836e-05]\n",
      "<NDArray 1 @cpu(0)>\n",
      "\n",
      "[ 0.]\n",
      "<NDArray 1 @cpu(0)>\n"
     ]
    }
   ],
   "source": [
    "print(x)  # 1000次迭代后x的取值\n",
    "print(x.grad)\n",
    "print(f(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:30:06.402454Z",
     "start_time": "2018-02-03T15:30:06.381525Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9848785337744963\n"
     ]
    }
   ],
   "source": [
    "r = np.random.rand() # [0, 1]均匀分布\n",
    "beta = 1-10**(- r - 1)\n",
    "print(beta)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## numpy 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:52:15.330829Z",
     "start_time": "2018-02-03T15:52:15.323022Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(20).reshape((5,4))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:30:43.444174Z",
     "start_time": "2018-02-03T15:30:43.440052Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [12 13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "print(a[[0, 1, 3], :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:32:20.065772Z",
     "start_time": "2018-02-03T15:32:20.058926Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[[0, 1, 3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:36:25.504573Z",
     "start_time": "2018-02-03T15:36:25.500559Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "row_idx = np.array([0, 1, 3])\n",
    "col_idx = np.array([0, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:52:30.374873Z",
     "start_time": "2018-02-03T15:52:30.367594Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2],\n",
       "       [ 4,  6],\n",
       "       [12, 14]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.ix_([0,1,3], [0,2])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:53:32.535164Z",
     "start_time": "2018-02-03T15:53:32.530998Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "def convert_to_one_hot(Y, C):\n",
    "    \"\"\"\n",
    "    Y是一个numpy.array vector, C是分类的种数\n",
    "    \"\"\"\n",
    "    Y = np.eye(C)[Y.reshape(-1)].T\n",
    "    return Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T16:21:40.488877Z",
     "start_time": "2018-02-03T16:21:40.477304Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 6)\n",
      "(6,)\n",
      "[[ 0.  0.  0.  1.  0.  0.]\n",
      " [ 1.  0.  0.  0.  0.  1.]\n",
      " [ 0.  1.  0.  0.  1.  0.]\n",
      " [ 0.  0.  1.  0.  0.  0.]]\n"
     ]
    }
   ],
   "source": [
    "y = np.array([[1, 2, 3, 0, 2, 1]])\n",
    "print(y.shape)\n",
    "print(y.reshape(-1).shape)\n",
    "C = 4\n",
    "print(convert_to_one_hot(y, C))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T16:23:05.370151Z",
     "start_time": "2018-02-03T16:23:05.361098Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  0,  2,  1, 12, 21, 31, 10, 21, 18,  1,  2,  3,  0,  2,\n",
       "        1, 10, 26, 36, 60, 27, 91])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.array([[[1, 2, 3, 0, 2, 1], [12, 21, 31, 10, 21, 18]], [[1, 2, 3, 0, 2, 1], [10, 26, 36, 60, 27, 91]]])\n",
    "y.reshape(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T16:23:20.039990Z",
     "start_time": "2018-02-03T16:23:19.986411Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.reshape?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
